package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.cache.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 攻略接口实现类
 */
@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepository strategyRepository;
    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IDestinationService destinationService;


    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        strategyRepository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        strategyRepository.save(strategy);
    }

    @Override
    public void delete(String id) {
        strategyRepository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return strategyRepository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return strategyRepository.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        //查询对象
        Query query = new Query();
        if (StringUtils.hasLength(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //当有选择某个主题时
        if (StringUtils.hasLength(qo.getThemeId())) {
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");

        return DBHelper.query(template, Strategy.class, query, pageable);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        StrategyCatalog catalog = strategyCatalogService.get(strategy.getCatalogId());
        //设置目的地id,名字
        strategy.setDestId(catalog.getDestId());
        //设置目的地名字
        strategy.setDestName(catalog.getDestName());
        //设置攻略主题名字
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(strategyTheme.getName());
        //设置攻略分类
        strategy.setCatalogName(catalog.getName());
        //是否国外
        List<Destination> toasts = destinationService.queryToasts(catalog.getDestId());
        if (toasts != null && toasts.size() > 0) {
            Destination destination = toasts.get(0);
            if ("中国".equals(destination)) {
                strategy.setIsabroad(Strategy.ABROAD_NO);
            } else {
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
        }
        //判断是否带id
        if (StringUtils.hasLength(strategy.getId())) {
            Strategy temp = this.get(strategy.getId());
            strategy.setCreateTime(temp.getCreateTime());
            strategy.setFavornum(temp.getFavornum());
            strategy.setReplynum(temp.getReplynum());
            strategy.setSharenum(temp.getSharenum());
            strategy.setViewnum(temp.getViewnum());
            strategy.setThumbsupnum(temp.getThumbsupnum());
            //有则编辑
            this.update(strategy);
        } else {
            //无则添加
            strategy.setCreateTime(new Date());//创建时间
            this.save(strategy);
        }
    }

    @Override
    public void changeState(String id, int state) {
        //部分字段更新
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("state", state);
        template.updateMulti(query, update, Strategy.class);
    }

    @Override
    public List<Strategy> queryCatalogId(String id) {

        return strategyRepository.findByCatalogId(id);
    }

    @Override
    public List<Strategy> queryViewNumTop3(String destId) {
        //显示前三，转换成将所有攻略进行分页，排序
        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");

        List<Strategy> list = strategyRepository.findByDestId(destId, pageable);
        return list;
    }

    @Override
    public void updateStrategyVo(StrategyStatisVO vo) {
        Query query = new Query();

        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));

        Update update = new Update();
        update.set("viewnum",vo.getViewnum());
        update.set("replynum",vo.getReplynum());
        update.set("favornum",vo.getFavornum());
        update.set("sharenum",vo.getSharenum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> queryByDestName(String destName) {


        return strategyRepository.findByDestName(destName);
    }
}
